package com.ruoyi.merchant.service.impl;

import com.ruoyi.common.core.cg.pojo.*;
import com.ruoyi.merchant.mapper.*;
import com.ruoyi.merchant.service.SynthesisService;
import com.ruoyi.merchant.utils.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
* @author ？？？
* @description 针对表【synthesis】的数据库操作Service实现
* @createDate 2025-03-04 14:04:13
*/
@Service
@Transactional
@Slf4j
public class SynthesisServiceImpl
    implements SynthesisService {
    @Autowired
    private SynthesisMapper synthesisMapper;
    @Autowired
    private LeaderMapper leaderMapper;
    @Autowired
    private CommunityMapper communityMapper;
    @Autowired
    private StoreMapper storeMapper;
    @Autowired
    private TCommunityLeaderMapper communityLeaderMapper;
    @Autowired
    private TLeaderStoreMapper leaderStoreMapper;

    @Override
    @Transactional
    public Result SynthesSave(Synthesis synthesis) {
        try {
            // 验证数据完整性
            if (synthesis.getUserId() == null) {
                throw new IllegalArgumentException("用户 ID 不能为空");
            }
            //leader表
            Leader leader = new Leader();
            leader.setId(synthesis.getUserId());
            leader.setName(synthesis.getUserName());
            leader.setIdcard(synthesis.getCardId());
            leader.setInvitationCode(synthesis.getInvitationCode());
            leader.setMoney(BigDecimal.ZERO);
            leader.setCreateTime(new Date());
            leaderMapper.updateLeader(leader);

            //根据qqmapid查询community表
            Community qqMapId = communityMapper.selectQQMapId(synthesis.getQqMapId());
            Integer communityId =null;
            //判断qqmapid不为null，修改community表
            if(qqMapId == null){
                //community表
                Community community = new Community();
                community.setTitle(synthesis.getScholarId());
                community.setAddress(synthesis.getAddress());
                community.setLeaderId(synthesis.getUserId());
                community.setCreateTime(new Date());
                community.setLat(synthesis.getLat());
                community.setLng(synthesis.getLng());
                community.setQqMapId(synthesis.getQqMapId());
                communityMapper.communitySave(community);
                communityId = community.getId();
            }else{
                Integer leaderId = qqMapId.getLeaderId();
                communityId = qqMapId.getId();
                if(leaderId == null){
                    Community community = new Community();
                    System.err.println(synthesis.getUserId());
                    community.setQqMapId(synthesis.getQqMapId());
                    community.setLeaderId(synthesis.getUserId());
                    communityMapper.CommunityUpdate(community);
                }
            }

            //store表
            Store store = new Store();
            store.setName(synthesis.getSynthesisName());
            store.setType(synthesis.getStoreId());
            store.setAddress(synthesis.getAddress());
            store.setPrice(BigDecimal.ZERO);
            store.setCreateTime(new Date());
            store.setUpdateTime(new Date());
            store.setStatus(2);
            storeMapper.storeSave(store);
            Integer storeId = store.getId();

            //t_community_leader表
            TCommunityLeader tCommunityLeader = new TCommunityLeader();
            tCommunityLeader.setCommunityId(communityId);
            tCommunityLeader.setLeaderId(synthesis.getUserId());
            communityLeaderMapper.comLeaderSave(tCommunityLeader);

            //t_leader_store表
            TLeaderStore tLeaderStore = new TLeaderStore();
            tLeaderStore.setLeaderId(synthesis.getUserId());
            tLeaderStore.setStoreId(storeId);
            leaderStoreMapper.leaderStoreSave(tLeaderStore);

            TCommunityStore tCommunityStore = new TCommunityStore();
            tCommunityStore.setCommunityId(communityId);
            tCommunityStore.setStoreId(storeId);
            synthesisMapper.saveComStore(tCommunityStore);
            return Result.ok();
        } catch (Exception e) {
            log.error("合成保存操作失败，错误信息: {}", e.getMessage(), e);
            // 返回错误结果
            return Result.error("合成保存操作失败，请检查输入数据");
        }
}

    @Override
    public Synthesis findByUserId(String userId) {
        return synthesisMapper.findByUserId(userId);
    }
}




